import Layout from '@/layout/index.vue';
import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import {useTestStore} from '../store'
const routes:Array<RouteRecordRaw> = [
 


    {
        //地址和component是必传的
        path:'/Main',
        name:'Main',
        component:()=>import('@/views/Main/index.vue'),
        meta:{requiresAuth:true}
        
    },
    {
        //地址和component是必传的
        path:'/FindAreas',
        name:'FindAreast',
        component:()=>import('@/views/FindAreas/FindAreas.vue')
    },
   
    {
        path:'/login',
      
        component:()=>import('@/views/Login/index.vue'),
        children:[
            {
                path:'/login',
                name:'Login',
                component:()=>import('@/views/Login/login.vue'),
              
            },
            {
                path:'/Reg',
                name:'/Reg',
                component:()=>import('@/views/Login/register.vue'),
              
            },
            {
                path:'/Forget',
                name:'/Forget',
                component:()=>import('@/views/Forget/forget.vue'),
              
            },
           
        ]
    },
    {
        path:'/Create',
        name:'Create',
        component:()=>import('@/views/create/create.vue'),
      
    },
    {
        path:'/Identity',
        name:'Identity',
        component:()=>import('@/views/Identity/Identity.vue'),
      
    },
       {
        path:'/Region',
        name:'Region',
        component:()=>import('@/views/Region/Region.vue'),
      
    },
   

    {
        path:'/behind',
      
        name:'Index',
        component:Layout,
        redirect:'/behind/index',
        children:[
            {
                
                    path: 'index',
                    name: 'index',
                    component: () => import('@/views/index/Index.vue')
              
            }
        ]
    },
    {
        path:'/user',
        redirect:'/user/manger',
        name:'User',
        component:Layout,
        children:[
            {
                
                    path: 'manger',
                    name: 'UserMange',
                    component: () => import('@/views/users/Users.vue')
              
            }
        ]
    },
   
    {
        
    path:'/order',
   
    name:'Order',
    redirect:'/order',
        component:Layout,
        
        children:[
            {
                path:'orderQuery',
                name:'orderQuery',    
                    component: () => import('@/views/orders/OrderQuery.vue')
              
            },
            {
                path:'orderAction',
                name:'orderAction',     
                component: () => import('@/views/orders/orderAction.vue')
              
          
        }
           
        ]
    },
    
     
    {
        
        path:'/goods',
        name:'Goods',
        redirect:'/goods',
            component:Layout,
            
            children:[
                {
                    path:'goodsQuery',
                    name:'goodsQuery',    
                        component: () => import('@/views/goods/GoodsQuery.vue')
                  
                },
                {
                    path:'goodsAction',
                    name:'goodsAction',       
                    component: () => import('@/views/goods/GoodsAction.vue')
                  
              
            }
               
            ]
        },
 

]



const router = createRouter (
    {
        history:createWebHistory(),
        routes:routes
    }
)
router.beforeEach((to, from) => {
    // 而不是去检查每条路由记录
   
    // to.matched.some(record => record.meta.requiresAuth)
    // 这里需要空的
    const Test=useTestStore()
    const {name} =Test
    // console.log('name is')
    // console.log(name)
    if (to.meta.requiresAuth&&!name) {
        // console.log('to.meta.requiresAuth')
        // console.log(useTestStore.name+'name!!!!!!11')
      // 此路由需要授权，请检查是否已登录
      // 如果没有，则重定向到登录页面
      return {
        path: '/',
        // 保存我们所在的位置，以便以后再来
    
        query: { redirect: to.fullPath },
      }

     
    }
  })


export default router